package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.annotation.pagehelper.page.Page;
import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.vo.PackagesView;
import cn.yj.cgs.dba.entity.vo.PackagesVo;
import cn.yj.common.Enums;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.iservice.IPackageService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 其它快递接口
 *
 * @author 永健
 * @since 2021-04-20 15:31
 */
@RequestMapping("/packages")
@RestController
public class PackagesController extends AppBaseController<PackagesView> {

    @Resource
    private IPackageService iPackageService;

    /**
     * 数据展示列表
     */
    @GetMapping("/pageList")
    @Anonymous
    public R pageList(@RequestParam Map<String, Object> params) {
        params.put("packageStatus", Enums.TASK.STATUS.no.name());
        return success(iPackageService.pageList(params, page(new OrderBy(OrderBy.Direction.DESC, "create_time"))));
    }

    /**
     * 分享页面的Id查询
     *
     * @param taskNo 编号
     */
    @GetMapping("/getOneByNo/{taskNo}")
    @Anonymous
    public R getOneByNo(@PathVariable("taskNo") String taskNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("packageNo", taskNo);
        Page page = iPackageService.pageList(params, page(new OrderBy(OrderBy.Direction.DESC, "create_time")));
        return success(page.getRows().isEmpty()? null : page.getRows().get(0));
    }


    @GetMapping("/myPushTask")
    public R myPushTask(@RequestParam Map<String, Object> params) {
        params.put("userCode", getUserCode());
        return success(iPackageService.getTaskListByUserCode(page(new OrderBy(OrderBy.Direction.DESC, "create_time"))
                , params));
    }

    /**
     * 接单
     *
     * @param taskNo 编号
     */
    @PostMapping("/toAgree/{taskNo}")
    @OperateLog(describe = "快递接单",type = LogType.UPDATE)
    public R publish(@PathVariable String taskNo) {
        return success(iPackageService.toAgree(getUserCode(), taskNo));
    }


    @PostMapping("/publish")
    @OperateLog(describe = "快递发布",type = LogType.INSERT)
    public R publish(@RequestBody @Valid PackagesVo task) {
        task.setUserCode(getUserCode());
        return iPackageService.publish(task);
    }

    @PostMapping("/updateTask")
    @OperateLog(describe = "快递更改",type = LogType.DELETE)
    public R updateTask(@RequestBody @Valid PackagesVo task) {
        task.setUserCode(getUserCode());
        return iPackageService.updateInfoById(task);
    }

    /**
     * 同意接单 状态更改为 doing
     *
     * @param taskNo
     */
    @OperateLog(describe = "快递-同意接单",type = LogType.UPDATE)
    @PostMapping("/agreeTask/{taskNo}")
    public R agreeTask(@PathVariable String taskNo) {
        return result(iPackageService.agreeTask(taskNo));
    }

    @OperateLog(describe = "不同意接收任务",type = LogType.UPDATE)
    @RequestMapping("/disAgreeTask/{taskNo}")
    public R disAgreeTask(@PathVariable String taskNo) {
        return result(iPackageService.disAgreeTask(getUserCode(), taskNo));
    }


    @OperateLog(describe = "取消快递",type = LogType.UPDATE)
    @RequestMapping("/cancelTask/{taskNo}")
    public R cancelTask(@PathVariable String taskNo) {
        return result(iPackageService.cancelTask(taskNo));
    }

    @OperateLog(describe = "快递完成",type = LogType.UPDATE)
    @RequestMapping("/finishTask/{taskNo}")
    public R finishTask(@PathVariable String taskNo) {
        return result(iPackageService.finishTask(getUserCode(), taskNo));
    }


    @GetMapping("/getByTaskNo/{taskNo}")
    @Anonymous
    public R getByTaskNo(@PathVariable String taskNo) {
        return success(iPackageService.getByTaskNo(taskNo));
    }

    @GetMapping("/listMyDoTask")
    public R listMyDoTask(@RequestParam Map<String, Object> params) {
        params.put("myUserCode", getUserCode());
        return success(iPackageService.listMyDoTask(PageUtils.page(new OrderBy(OrderBy.Direction.DESC, "create_time")), params));
    }

    @OperateLog(describe = "删除我做的快递",type = LogType.DELETE)
    @DeleteMapping("/delMyDoTask/{taskNos}")
    public R delMyDoTask(@PathVariable List<String> taskNos) {
        return result(iPackageService.delMyDoTask(taskNos));
    }

    @OperateLog(describe = "删除我发布的快递",type = LogType.DELETE)
    @DeleteMapping("/delMyPushTask/{taskNos}")
    public R delMyPushTask(@PathVariable List<String> taskNos) {
        return result(iPackageService.delMyPushTask(taskNos));
    }


}
